package cn.mumzone.pos.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.listener.OnTimeSelectListener;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.mumzone.basic.base.BaseActivity;
import cn.mumzone.basic.base.BaseFragment;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.IsFastClickUtil;
import cn.mumzone.pos.R;
import cn.mumzone.pos.ui.fragment.FRA_TransactionRecord;
import cn.mumzone.pos.ui.widgets.PosTimePickerView;
import cn.mumzone.pos.ui.widgets.tablayout.MagicIndicator;
import cn.mumzone.pos.ui.widgets.tablayout.ViewPagerHelper;
import cn.mumzone.pos.ui.widgets.tablayout.buildins.commonnavigator.CommonNavigator;
import cn.mumzone.pos.ui.widgets.tablayout.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import cn.mumzone.pos.ui.widgets.tablayout.buildins.commonnavigator.abs.IPagerIndicator;
import cn.mumzone.pos.ui.widgets.tablayout.buildins.commonnavigator.abs.IPagerTitleView;
import cn.mumzone.pos.ui.widgets.tablayout.buildins.commonnavigator.indicators.LinePagerIndicator;
import cn.mumzone.pos.ui.widgets.tablayout.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import cn.mumzone.pos.ui.widgets.tablayout.buildins.commonnavigator.titles.SimplePagerTitleView;

/**
 * Created by qiuzhenhuan on 2019/8/1.
 */

public class ACT_MemberTransactionRecord extends BaseActivity {

    private static final String KEY_MEMBER_ID = "member_id";

    @BindView(R.id.indicator_transaction_record)
    MagicIndicator idcTransactionRecord;
    @BindView(R.id.vp_transaction_record)
    ViewPager vpTransactionRecord;

    @BindView(R.id.tv_day)
    TextView tvDay;
    @BindView(R.id.ic_go_previous_day)
    ImageView ivGoPreviousDay;
    @BindView(R.id.ic_go_next_day)
    ImageView ivGoNextDay;


    private List<String> titles;

    private PosTimePickerView posTimePickerView;

    private Date currentDay;

    private String memberId;

    private List<FRA_TransactionRecord> fragments = new ArrayList<>();

    private CommonNavigator commonNavigator;


    @Override
    protected void initVariable() {
        titles = new ArrayList<>();
        titles.add("全部");
        titles.add("充值");
        titles.add("消费");
        titles.add("退款");
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        memberId = extras.getString(KEY_MEMBER_ID);
    }

    @Override
    protected void initEventAndData() {

        fragments.add(FRA_TransactionRecord.getInstance(FRA_TransactionRecord.TYPE_ALL, memberId));
        fragments.add(FRA_TransactionRecord.getInstance(FRA_TransactionRecord.TYPE_RECHARGE, memberId));
        fragments.add(FRA_TransactionRecord.getInstance(FRA_TransactionRecord.TYPE_CONSUMPTION, memberId));
        fragments.add(FRA_TransactionRecord.getInstance(FRA_TransactionRecord.TYPE_REFUND, memberId));
        vpTransactionRecord.setOffscreenPageLimit(fragments.size());
        vpTransactionRecord.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                FRA_TransactionRecord baseFragment = fragments.get(position);
                return baseFragment;
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });
        vpTransactionRecord.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        initIndicator();

        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        int nowYear = endDate.get(Calendar.YEAR);
        startDate.set(nowYear - 2, 0, 1);
        posTimePickerView = new PosTimePickerView.PosTimePickerBuilder(mContext, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                currentDay = date;
                setDateChanged(date);
            }
        }).isDialog(true).isCyclic(false).setOutSideCancelable(true)
                .setTitleText("选择时间")
                .setType(new boolean[]{true, true, true, false, false, false})
                .setDate(endDate)
                .setRangDate(startDate, endDate)
                .build();

        setDateChanged(endDate.getTime());

    }

    private void setDateChanged(Date currentDay) {
        tvDay.setText(String.format("%s %s", AppDateUtil.getDateToString(currentDay.getTime(), AppDateUtil.YYYY_MM_DD1),
                AppDateUtil.getWeekOfDate(currentDay.getTime())));
        if (AppDateUtil.isSameDay(currentDay, new Date())) {
            ivGoNextDay.setVisibility(View.INVISIBLE);
        } else {
            ivGoNextDay.setVisibility(View.VISIBLE);
        }
        if (null != this.currentDay) {
            FRA_TransactionRecord fragment = getCurrentFragment();
            fragment.refresh();
        }
        this.currentDay = currentDay;

    }


    public FRA_TransactionRecord getCurrentFragment() {
        for (int index = 0; index < fragments.size(); index++) {
            FRA_TransactionRecord fragment = fragments.get(index);
            fragment.setDateTime(currentDay.getTime());
            if (index == vpTransactionRecord.getCurrentItem() && !fragment.isHidden()) {
                return fragment;
            }
        }
        return null;
    }

    private void initIndicator() {

        idcTransactionRecord.setBackgroundColor(Color.WHITE);
        commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setNormalColor(Color.parseColor("#FF333333"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#FF333333"));
                simplePagerTitleView.setText(titles.get(index));
                simplePagerTitleView.setTextSize(18);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        vpTransactionRecord.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setColors(Color.parseColor("#FF28282C"));
                return linePagerIndicator;
            }

        });
        idcTransactionRecord.setNavigator(commonNavigator);
        ViewPagerHelper.bind(idcTransactionRecord, vpTransactionRecord);
        commonNavigator.onPageSelected(0);
    }

    @OnClick({R.id.tv_day, R.id.ic_go_previous_day, R.id.ic_go_next_day})
    void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.ic_go_previous_day:
                if (getCurrentFragment().isRefreshing()) {
                    return;
                }
                currentDay = new Date(AppDateUtil.getPreviousDay(currentDay.getTime()));
                setDateChanged(currentDay);
                break;
            case R.id.ic_go_next_day:
                if (getCurrentFragment().isRefreshing()) {
                    return;
                }
                currentDay = new Date(AppDateUtil.getNextDay(currentDay.getTime()));
                setDateChanged(currentDay);
                break;
            case R.id.tv_day:
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(currentDay);
                posTimePickerView.setDate(calendar);
                posTimePickerView.show();
                break;
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.act_member_transaction_record;
    }

    public static void startAc(Context context, String memberId) {
        Intent intent = new Intent(context, ACT_MemberTransactionRecord.class);
        intent.putExtra(KEY_MEMBER_ID, memberId);
        context.startActivity(intent);
    }
}
